home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / tools / czesc_4 / showprefs / showprefs.c < prev    next >
C/C++ Source or Header  |  1992-03-15  |  7KB  |  204 lines

  1. /************************************************************************
  2. **                                                                     **
  3. ** ShowPreferences                                                     **
  4. **                                                                     **
  5. ** This utility allows you to view the system-configuation file of any **
  6. ** workbench disk without booting from this disk.                      **
  7. ** ShowPrefs can be started from CLI or workbench.                     **
  8. **                                                                     **
  9. ** Author & date: Rainer Koppler, 24.2.1992                            **
  10. ** Compiler: Lattice-C v5.02                                           **
  11. **                                                                     **
  12. ************************************************************************/
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/preferences.h>
  18. #include <workbench/icon.h>
  19. #include <workbench/startup.h>
  20. #include <workbench/workbench.h>
  21. #include <libraries/dosextens.h>
  22.  
  23. #define LIB_VERSION 33L  /* at least Kickstart 1.2 */
  24. #define MAX_LEN 80       /* length of work string */
  25.  
  26. #define PREF_SIZE sizeof(struct Preferences)
  27.  
  28. /*---------------------------------------------------------------------**
  29. ** external variables and global declarations                          **
  30. **---------------------------------------------------------------------*/
  31.  
  32. extern struct IntuitionBase *IntuitionBase;
  33. extern struct IconBase *IconBase;
  34. extern struct WBStartup *WBenchMsg;
  35.  
  36. static char workStr[MAX_LEN];
  37. static char devStr[MAX_LEN/2];
  38. static const char sysConfName[] = "system-configuration";
  39. static char toolName[] = "UNIT";
  40.  
  41.  
  42. /*---------------------------------------------------------------------**
  43. ** function: GetToolType                                               **
  44. ** returns the value of a specified tool type of a specified file.     **
  45. **---------------------------------------------------------------------*/
  46.  
  47. char *GetToolType (char *fileName, char *ttName, char *ttValue)
  48. {
  49.  struct DiskObject *obj;
  50.  char *ttVal = NULL;
  51.  
  52.  if(IconBase = (struct IconBase *)OpenLibrary("icon.library",LIB_VERSION)) {
  53.    if(obj = GetDiskObject(fileName)) {
  54.      if(ttVal = FindToolType(obj->do_ToolTypes,ttName)) {
  55.        strcpy(ttValue,ttVal);
  56.        ttVal = ttValue;
  57.      }
  58.      FreeDiskObject(obj);
  59.    }
  60.    CloseLibrary(IconBase);
  61.  }
  62.  else
  63.    printf("Cannot open icon.library.\n");
  64.  return(ttVal);
  65. }
  66.  
  67.  
  68. /*----------------------------------------------------------------------**
  69. ** function: GetPrefName                                                **
  70. ** searches a file looking like "<unitName>[devs/]system-configuration" **
  71. ** and returns the name of the first found file.                        **
  72. **----------------------------------------------------------------------*/
  73.  
  74. char *GetPrefName(char *unitName)
  75. {
  76.  BOOL found = FALSE;
  77.  struct FileLock *tst;
  78.  
  79.  /* attempt to access UNIT:system-configuration */
  80.  if(unitName != NULL) strcpy(workStr,unitName);
  81.  strcat(workStr,sysConfName);
  82.  if((tst = (struct FileLock *)Lock(workStr,ACCESS_READ)) != NULL)
  83.    found=TRUE;
  84.  UnLock(tst);
  85.  if(found) return(workStr);
  86.  
  87.  /* attempt to access UNIT:devs/system-configuration */
  88.  workStr[0] = '\0';
  89.  if(unitName != NULL)
  90.    strcpy(workStr,unitName);
  91.  strcat(workStr,"devs/");
  92.  strcat(workStr,sysConfName);
  93.  if((tst = (struct FileLock *)Lock(workStr,ACCESS_READ)) != NULL)
  94.    found=TRUE;
  95.  UnLock(tst);
  96.  if(found) return(workStr);
  97.  
  98.  /* no matching file found */
  99.  return(NULL);
  100. }
  101.  
  102.  
  103. /*----------------------------------------------------------------------**
  104. ** function: ShowPrefs                                                  **
  105. ** sets new preferences, opens a simple window and waits until the user **
  106. ** closes the window.                                                   **
  107. **----------------------------------------------------------------------*/
  108.  
  109. struct NewWindow demoWindow = {
  110.   185,82,                  /* LeftEdge,TopEdge   */
  111.   270,92,                  /* Width,Height       */
  112.   0,1,                     /* DetailPen,BlockPen */
  113.   CLOSEWINDOW,             /* IDCMPFlags         */
  114.   WINDOWDRAG+WINDOWCLOSE+
  115.   WINDOWSIZING+WINDOWDEPTH,/* Window Flags       */
  116.   NULL,                    /* Gadget             */
  117.   NULL,                    /* Image              */
  118.   "Close window to exit",  /* Title              */
  119.   NULL,                    /* Screen             */
  120.   NULL,                    /* Bitmap             */
  121.   248,20,                  /* MinWidth,MinHeight */
  122.   640,256,                 /* MaxWidth,MaxHeight */
  123.   WBENCHSCREEN             /* Type               */
  124. };
  125.  
  126.  
  127. void ShowPrefs(char *prefName)
  128. {
  129.  struct Preferences *oldPrefs,*newPrefs,*setPrefs;
  130.  struct FileHandle *pFile;
  131.  struct Window *win;
  132.  struct IntuiMessage *imsg;
  133.  int class,code;
  134.  
  135.  if(IntuitionBase=(struct IntuitionBase *) OpenLibrary("intuition.library",LIB_VERSION)) {
  136.    if(oldPrefs=(struct Preferences *)AllocMem(PREF_SIZE*3,MEMF_CLEAR|MEMF_FAST)) {
  137.      newPrefs = oldPrefs+PREF_SIZE;
  138.      setPrefs = newPrefs+PREF_SIZE;
  139.      if(pFile = (struct FileHandle *)Open(prefName,MODE_OLDFILE)) {
  140.        Read(pFile,(UBYTE *)newPrefs,PREF_SIZE);
  141.        Close(pFile);
  142.        GetPrefs(oldPrefs,PREF_SIZE);
  143.        CopyMem(oldPrefs,setPrefs,PREF_SIZE);
  144.        setPrefs->color17 = newPrefs->color17;
  145.        setPrefs->color18 = newPrefs->color18;
  146.        setPrefs->color19 = newPrefs->color19;
  147.        CopyMem(newPrefs->PointerMatrix,setPrefs->PointerMatrix,sizeof(USHORT)*36);
  148.        setPrefs->color0 = newPrefs->color0;
  149.        setPrefs->color1 = newPrefs->color1;
  150.        setPrefs->color2 = newPrefs->color2;
  151.        setPrefs->color3 = newPrefs->color3;
  152.        SetPrefs(setPrefs,PREF_SIZE,FALSE);
  153.        if(win = (struct Window *)OpenWindow(&demoWindow)) {
  154.          Wait(1L << win->UserPort->mp_SigBit);
  155.          while(imsg = (struct IntuiMessage *)GetMsg(win->UserPort)) {
  156.            class = imsg->Class;
  157.            code = imsg->Code;
  158.            ReplyMsg(imsg);
  159.            if(class == CLOSEWINDOW) break;
  160.          }
  161.          CloseWindow(win);
  162.        }
  163.        SetPrefs(oldPrefs,PREF_SIZE,FALSE);
  164.      }
  165.      FreeMem(oldPrefs,PREF_SIZE*3);
  166.    }
  167.    CloseLibrary(IntuitionBase);
  168.  }
  169. }
  170.  
  171.  
  172. /*--------------------------------------------------------------------*/
  173.  
  174. void main(int argc, char *argv[])
  175. {
  176.  char *confName = NULL, *unitName = NULL;
  177.  
  178.  if(argc == 0)
  179.    /* start from workbench */
  180.    unitName = GetToolType(WBenchMsg->sm_ArgList->wa_Name,toolName,devStr);
  181.  else {
  182.    /* start from CLI */
  183.    if(--argc > 1) {
  184.      printf("Too many parameters !\n");
  185.      exit(FALSE);
  186.    }
  187.    if(argc == 1) {
  188.      if(strcmp(argv[1],"?")==0) {
  189.        printf("\n»» ShowPrefs «« by Rainer Koppler\n\n");
  190.        printf("Usage: %s [<system-configuration file>]\n",argv[0]);
  191.        exit(FALSE);
  192.      }
  193.      else
  194.        confName = argv[1];
  195.    }
  196.  }
  197.  if(confName == NULL)
  198.    if((confName = GetPrefName(unitName))==NULL) {
  199.      printf("No system-configuration file found.\n");
  200.      exit(FALSE);
  201.    }
  202.  ShowPrefs(confName);
  203. }
  204.